package com.whu.MenuHub.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.MenuHub.dao.FoodFlavorDao;
import com.whu.MenuHub.domain.Food;
import com.whu.MenuHub.domain.FoodFlavor;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.FoodFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodFlavorServiceImpl extends ServiceImpl<FoodFlavorDao, FoodFlavor> implements FoodFlavorService {
    @Autowired
    FoodFlavorDao foodFlavorDao;

    //获得一条口味偏好信息
    @Cacheable(cacheNames = "foodflavor",key = "#id",condition = "#id!=null")
    @Override
    public FoodFlavor getfalvorByFoodID(Integer id){
        return foodFlavorDao.selectById(id);
    }

    //新增一条口味偏好信息
    @Override
    public FoodFlavor insertFoodFalvor(FoodFlavor foodFlavor) throws MenuHubException{
        try{
            foodFlavorDao.insert(foodFlavor);
            return foodFlavor;
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.INSERT_ERROR,"添加食品口味偏好信息失败");
        }
    }

    @Override
    @CacheEvict(cacheNames = "foodflavor",key = "#foodFlavor.foodID")
    public void updateFoodFlavor(FoodFlavor foodFlavor) throws MenuHubException{
        try{
            foodFlavorDao.updateById(foodFlavor);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.UPDATE_ERROR,"修改食品口味偏好信息失败");
        }
    }

    @Override
    @CacheEvict(cacheNames = "foodflavor",key = "#id")
    public void deleteFoodFlavorById(Integer id) throws MenuHubException{
        try{
            foodFlavorDao.deleteById(id);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.DELETE_ERROR,"删除食品口味偏好信息失败");
        }
    }
    @Override
    public List<FoodFlavor> getAllFoodFlavor() {
        QueryWrapper<FoodFlavor> qw = new QueryWrapper<>();
        return foodFlavorDao.selectList(qw);
    }
}
